﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Core;
using BDPAutodetect.Device;
using BDPAutodetect.Models;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// DIDO输入输出板 检测任务
    /// </summary>
    public partial class DetectJobSensorSign : DetectJobBase
    {
        /// <summary>
        /// 检测设备
        /// </summary>
        protected DetectDeviceSensorSign? DetectDevice { get; set; }
        /// <summary>
        /// 最后一次Led灯类型
        /// </summary>
        protected LedLightType LastLedLight { get; set; }
        /// <summary>
        /// Led指标类型对应表
        /// </summary>
        protected Dictionary<LedLightType, DeviceTargetType> TargetLedLightPairs { get; set; }
        /// <summary>
        /// 启动开关状态
        /// </summary>
        public bool SwitchStatus => this.GetSwitchStatus();

        /// <summary>
        /// 构造函数
        /// </summary>
        public DetectJobSensorSign(DeviceStationType argStationType)
            : base(argStationType, $"{argStationType.ToInt()}号工位DIDO输入输出板任务", 100, false, false, true, true)
        {
            this.LastLedLight = LedLightType.None;
            this.TargetLedLightPairs = new Dictionary<LedLightType, DeviceTargetType>();
            this.TargetLedLightPairs.Add(LedLightType.Red, DeviceTargetType.SensorSign_LedLightRedWrite);
            this.TargetLedLightPairs.Add(LedLightType.Green, DeviceTargetType.SensorSign_LedLightGreenWrite);
            this.TargetLedLightPairs.Add(LedLightType.Yellow, DeviceTargetType.SensorSign_LedLightYellowWrite);
            //从容器中获取 - 检测设备管理初始化
            var detectDeviceManager = ContainerManager.Instance.GetContainerServiceRequired<DetectDeviceManager>();
            if (detectDeviceManager != null)
            {
                var detectDevice = detectDeviceManager.GetDetectDevice(DeviceType.SensorSign);
                if (detectDevice != null) this.DetectDevice = (DetectDeviceSensorSign)detectDevice;
            }
        }

        /// <summary>
        /// 启动检测
        /// </summary>
        public override async Task<bool> StartDetect()
        {
            await Task.CompletedTask;
            return true;
        }

        /// <summary>
        /// 停止检测
        /// </summary>
        public override async Task StopDetect()
        {
            await Task.CompletedTask;
        }

        /// <summary>
        /// 暂停检测
        /// </summary>
        public override async Task PauseDetect()
        {
            await Task.CompletedTask;
        }

        /// <summary>
        /// 继续检测
        /// </summary>
        public override async Task ContinueDetect()
        {
            await Task.CompletedTask;
        }

        /// <summary>
        /// 复位设备信号
        /// </summary>
        public void ResetSensor()
        {
            this.SetHummer(SwitchType.Off);
            foreach (var item in this.TargetLedLightPairs)
            {
                this.DetectDevice?.WriteDeviceData(this.StationType, item.Value, SwitchType.Off);
            }
        }

        /// <summary>
        /// 设置Led灯开启
        /// </summary>
        /// <param name="argLedLightType"></param>
        public void SetLedLightOn(LedLightType argLedLightType)
        {
            this.SetLedLight(argLedLightType, SwitchType.On);
        }

        /// <summary>
        /// 设置Led灯熄灭
        /// </summary>
        /// <param name="argLedLightType"></param>
        public void SetLedLightOff(LedLightType argLedLightType)
        {
            this.SetLedLight(argLedLightType, SwitchType.Off);
        }

        /// <summary>
        /// 设置Led灯
        /// </summary>
        /// <param name="argLedLightType">Led灯类型</param>
        /// <param name="argSwitchType">On/Off</param>
        public void SetLedLight(LedLightType argLedLightType, SwitchType argSwitchType)
        {
            //先检最后一次开启的灯颜色，有则关闭，再开启
            if (this.LastLedLight != LedLightType.None)
            {
                this.DetectDevice?.WriteDeviceData(this.StationType, this.TargetLedLightPairs[this.LastLedLight], SwitchType.Off);
            }
            this.DetectDevice?.WriteDeviceData(this.StationType, this.TargetLedLightPairs[argLedLightType], argSwitchType);
            this.LastLedLight = argSwitchType == SwitchType.On ? argLedLightType : LedLightType.None;
        }

        /// <summary>
        /// 设置蜂鸣器
        /// </summary>
        /// <param name="argSwitchType">On/Off</param>
        public void SetHummer(SwitchType argSwitchType)
        {
            this.DetectDevice?.WriteDeviceHummer(this.StationType, argSwitchType);
        }

        /// <summary>
        /// 获取检测设备启动开关状态
        /// </summary>
        /// <returns></returns>
        protected bool GetSwitchStatus()
        {
            if (this.DetectDevice == null) return false;
            switch (this.StationType)
            {
                case DeviceStationType.Station1: return this.DetectDevice.SwitchStatus.StationStatus1;
                case DeviceStationType.Station2: return this.DetectDevice.SwitchStatus.StationStatus2;
                case DeviceStationType.Station3: return this.DetectDevice.SwitchStatus.StationStatus3;
                default: return false;
            }
        }
    }
}
